package com.ellricky.baselib.ui

import android.view.View
import android.widget.AdapterView
import com.andr.common.http.callback.BaseCallback
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.ClueSelectAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.MyClueData
import com.ellricky.baselib.entity.new.MyClueList
import com.ellricky.baselib.ui.base.BaseRefreshActivity
import com.ellricky.baselib.ui.consutant.CtSchoolSelectActivity
import com.ellricky.baselib.ui.consutant.CtSelectActivity
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.util.jump
import kotlinx.android.synthetic.main.activity_clue_select.*

class ClueSelectActivity : BaseRefreshActivity<Void, BasePresenter<Void>, MyClueData>() {

    private val clueAdapter by lazy {
        ClueSelectAdapter()
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_clue_select
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = "多选分配线索"
        tvSelect.text = if (LocalCacheUtil.getUid() == BaseConstant.CARD_PRESIDENT) {
            "选择课程顾问"
        } else {
            "选择学校"
        }

        tvSelect.setOnClickListener {
            val list = arrayListOf<String>()
            dataSource.forEach {
                if (it.isSelect) {
                    list.add(it.clue_id)
                }
            }
            val ids = list.toString()
                .replace("[", "")
                .replace("]", "")
                .replace(" ", "")
            if (LocalCacheUtil.getUid() == BaseConstant.CARD_PRESIDENT) {
                jump<CtSelectActivity> {
                    putExtra(BaseConstant.KEY_ID, ids)
                }
            } else {
                jump<CtSchoolSelectActivity> {
                    putExtra(BaseConstant.KEY_ID, ids)
                }
            }
        }
    }

    override fun getResponseCallback(): BaseCallback? {
        return null
    }

    override fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEntityCallback<MyClueList>(this, true) {
            override fun onSuccess(data: MyClueList, var2: String?) {
                dataSource = data.data_list
                rvView.onLoadFinish(data.data_list, refresh)
                val selectList = dataSource.filter { it.isSelect }
                if (!selectList.isNullOrEmpty()) {
                    tv_select_number.text = "当前已选择  ${selectList.size}条"
                } else {
                    tv_select_number.text = "当前已选择  0条"
                }
            }
        }
    }

    override fun initParamMap() {
        super.initParamMap()
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["action"] = getAction()
        paramMap["eid"] = LocalCacheUtil.getUid()
    }

    private fun getAction(): String {
        when (LocalCacheUtil.getCardType()) {
            BaseConstant.CARD_PRESIDENT -> {
                return "headAllotList"
            }
            BaseConstant.CARD_MARKET -> {
                return "allotList"
            }
        }
        return ""
    }

    override fun onItemClick(adapterView: AdapterView<*>?, view: View?, position: Int, l: Long) {
        super.onItemClick(adapterView, view, position, l)
        val dataSource = adapter.dataSource
        val selectData = dataSource[position]
        selectData.isSelect = !selectData.isSelect
        adapter.notifyItemChanged(position)

        val selectList = dataSource.filter { it.isSelect }
        if (!selectList.isNullOrEmpty()) {
            tv_select_number.text = "当前已选择  ${selectList.size}条"
        }
    }

    override fun getRvAdapter(): BaseRecyclerAdapter<MyClueData> {
        return clueAdapter
    }

    override fun getUrlPath(): String {
        return ""
    }
}